﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace agilecards.Model
{
    public class DtoConverter
    {
        public DtoConverter(ICardCollectionFactory collectionFactory)
        {
            _collectionFactory = collectionFactory;
        }

        public CardCollectionDto ConvertToDto(ICardCollection collection)
        {
            CardCollectionDto collectionDto = new CardCollectionDto();
            collectionDto.Name = collection.Name;
            collectionDto.Id = collection.Id;

            List<CardDto> cardDtos = new List<CardDto>(collection.Cards.Count);

            foreach (Card card in collection.Cards)
            {
                CardDto cardDto = ConvertToDto(card);
                cardDtos.Add(cardDto);
            }

            collectionDto.Cards = cardDtos.ToArray();
            return collectionDto;
        }

        private static CardDto ConvertToDto(Card card)
        {
            CardDto cardDto = new CardDto();
            cardDto.Title = card.Title;
            cardDto.Description = card.Description;
            cardDto.Points = card.Points;
            cardDto.X = card.Position.X;
            cardDto.Y = card.Position.Y;
            return cardDto;
        }

        public ICardCollection ConvertFromDto(CardCollectionDto collectionDto)
        {
            ICardCollection collection = _collectionFactory.Create();
            collection.Name = collectionDto.Name;
            collection.Id = collectionDto.Id;

            foreach (CardDto cardDto in collectionDto.Cards)
            {
                Card card = ConvertFromDto(cardDto);
                collection.Cards.Add(card);
            }
            return collection;
        }

        private static Card ConvertFromDto(CardDto cardDto)
        {
            Card card = new Card();
            card.Title = cardDto.Title;
            card.Description = cardDto.Description;
            card.Points = cardDto.Points;
            card.Position = new Point(cardDto.X, cardDto.Y);

            return card;
        }

        ICardCollectionFactory _collectionFactory;
    }
}
